package com.gimi.cloud.bbp.manage.service.sys.impl;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.ReflectUtils;
import com.gimi.cloud.bbp.dao.dao.sys.SysResourceDao;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.bbp.dto.sys.SysResourceDTO;
import com.gimi.cloud.bbp.entity.sys.SysResourceEntity;
import com.gimi.cloud.bbp.manage.service.sys.SysResourceService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统资源路径相关接口实现类
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-01-30
 **/
@Service
public class SysResourceServiceImpl implements SysResourceService {
    private static final Logger logger = LoggerFactory.getLogger(SysResourceServiceImpl.class);
    private SysResourceDao sysResourceDao;

    @Autowired
    public SysResourceServiceImpl(SysResourceDao sysResourceDao) {
        this.sysResourceDao = sysResourceDao;
    }

    @Override
    public List<SysResourceDTO> queryList(SysResourceDTO dto, boolean isPaging) throws Exception {
        if(isPaging){
            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
        }
        String sql="SELECT res.* FROM sys_resource res WHERE res.is_delete = 0 ";
        QueryParam queryParam = new QueryParam();
        queryParam.setSql(sql);
        return sysResourceDao.selectListDTOBySql(queryParam);
    }

    @Override
    public SysResourceDTO load(SysResourceDTO dto, Object id) throws Exception {
        SysResourceEntity resource = sysResourceDao.queryBy("re_id", id);
        if (null == resource) {
            return null;
        }
        SysResourceDTO resourceDTO = new SysResourceDTO();
        ReflectUtils.copyPropertiesIgnoreNull(resource, resourceDTO);
        return resourceDTO;
    }

    @Override
    public SysResourceDTO saveOrUpdate(SysResourceDTO dto) throws Exception {
        SysResourceEntity resource = null;
        if (null != dto.getReId()) {
            resource = sysResourceDao.queryBy("re_id", dto.getReId());
            if (null == resource) {
                throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取资源id为" + dto.getReId() + "的资源信息，更新失败");
            }
        } else {
            resource = new SysResourceEntity();
        }
        ReflectUtils.copyPropertiesIgnoreNull(dto, resource);
        if (null != dto.getReId()) {
            sysResourceDao.baseUpdateByPrimaryKey(resource);
        } else {
            sysResourceDao.baseInsert(resource);
        }
        ReflectUtils.copyPropertiesIgnoreNull(resource, dto);
        return dto;
    }

    @Override
    public int delete(SysResourceDTO dto, Object id, boolean realDel) throws Exception {
        SysResourceEntity resource = sysResourceDao.queryBy("re_id", id);
        if (null == resource) {
            throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取主键为" + id + "的资源信息，删除失败");
        }
        //非物理删除
        if (!realDel) {
            ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(dto, resource);
            resource.setIsDelete(1);
            return sysResourceDao.baseUpdateByPrimaryKey(resource);
        }
        return sysResourceDao.deleteByPrimaryKey(id);
    }
}
